package com.fa.wisdom.ui;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.coder.zzq.smartshow.dialog.LoadingDialog;
import com.fa.wisdom.R;
import com.fa.wisdom.base.BaseActivity;
import com.fa.wisdom.model.ResultEntity;
import com.fa.wisdom.net.MyCallback;
import com.fa.wisdom.net.Task;
import com.fa.wisdom.utils.CountTimer;
import com.fa.wisdom.utils.Loading;
import com.fa.wisdom.utils.TitleBuilder;
import com.fa.wisdom.utils.ToastUtils;

import retrofit2.Response;

public class ResetPwdActivity extends BaseActivity {
    EditText et_password;
    EditText et_phone;
    EditText et_phoneCode;
    TextView tv_getcode;
    CountTimer countTimer;
    ImageView ivHideOr;
    private boolean isHide = true;//明文和密码
    @Override
    public void setListen() {
        new TitleBuilder(this).setTitleText("重置密码")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
        countTimer = new CountTimer(tv_getcode);
    }

    @Override
    public void setTitleBarColor() {

    }
    public void send() {
        String p = et_phone.getText().toString();
        if (TextUtils.isEmpty(p)) {
            ToastUtils.showError("请输入正确的手机号！");
            return;
        }
        LoadingDialog loadingDialog = Loading.getInstance().onShowLargeLoading( "正在发送...");
        loadingDialog.showInActivity(this);
        countTimer.start();
        Task.getApiService().sendPhoneCode(p).enqueue(new MyCallback<ResultEntity>() {
            @Override
            protected void onSuccess(Response<ResultEntity> response) {
                loadingDialog.dismiss();
                String jsonString = JSON.toJSONString(response.body());
                JSONObject jsonObject = JSON.parseObject(jsonString);
                ToastUtils.showSuccess(jsonObject.getString("msg"));
            }

            @Override
            protected void onFailure(Throwable t) {
                ToastUtils.showError(t.getMessage());
                loadingDialog.dismiss();
            }
        });
    }
    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }
    @Override
    public int intiLayout() {
        return R.layout.activity_reset_pwd;
    }
    public void resetPwd(View view){
        //重置密码
        String p = et_password.getText().toString();
        String ph = et_phone.getText().toString();
        String co = et_phoneCode.getText().toString();
        if (TextUtils.isEmpty(p) || TextUtils.isEmpty(ph) || TextUtils.isEmpty(co)) {
            ToastUtils.showError("以上信息不能为空");
            return;
        }
        LoadingDialog loadingDialog = Loading.getInstance().onShowLargeLoading( "正在注册...");
        loadingDialog.showInActivity(this);
        Task.getApiService().resetPwd(ph, co, p).enqueue(new MyCallback<ResultEntity>() {
            @Override
            protected void onSuccess(Response<ResultEntity> response) {
                loadingDialog.dismiss();
                String jsonString = JSON.toJSONString(response.body());
                JSONObject jsonObject = JSON.parseObject(jsonString);
                ToastUtils.showSuccess(jsonObject.getString("msg"));
                finish();
            }

            @Override
            protected void onFailure(Throwable t) {
                ToastUtils.showError(t.getMessage());
                loadingDialog.dismiss();
            }
        });
    }
    @Override
    public void initView() {
        tv_getcode = findViewById(R.id.tv_getcode);
        et_password = findViewById(R.id.et_password);
        et_phone = findViewById(R.id.et_phone);
        et_phoneCode = findViewById(R.id.et_phoneCode);
        ivHideOr = findViewById(R.id.iv_eye);
        ivHideOr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isHide) {
                    //隐藏
                    ivHideOr.setImageResource(R.drawable.login_eyehide);
                    et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    //显示
                    ivHideOr.setImageResource(R.drawable.login_eye);
                    et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                isHide = !isHide;
                et_password.setSelection(et_password.getText().length());
            }
        });
        tv_getcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send();
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countTimer!=null)
            countTimer.cancel();
    }
}
